package com.hyc.myvideo.ui.fragment.home;

import android.widget.ListView;
import butterknife.BindView;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.TrainingRecordAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.PracticeRecord;
import com.hyc.myvideo.data.api.model.PracticeRecordModel;
import com.hyc.myvideo.ui.base.BaseFragment;
import java.util.ArrayList;
import java.util.List;

/**
 * 修炼记录
 * Created by dfeads on 2017/7/13.
 */

public class TrainingRecordFragment extends BaseFragment {
  @BindView(R.id.fragment_teacher_pull_refresh) PullToRefreshListView mPullRefresh;

  private int page = 1;
  private TrainingRecordAdapter trainingRecordAdapter;
  private List<PracticeRecord> practiceRecordList;

  @Override protected int getLayoutId() {
    return R.layout.fragment_teacher;
  }

  @Override protected void initView() {

    practiceRecordList = new ArrayList<>();
    trainingRecordAdapter = new TrainingRecordAdapter(getContext(), practiceRecordList);
    mPullRefresh.setAdapter(trainingRecordAdapter);

    mPullRefresh.setMode(PullToRefreshBase.Mode.BOTH);
    mPullRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

      }
    });
  }

  @Override protected void initData() {
    api.getPracticeRecord(userInfo.getUser_id(), page);
  }

  @Override protected void onNexts(String result, String method) {
    BaseResult<PracticeRecordModel> practiceRecordModel =
        JSONObject.parseObject(result, new TypeReference<BaseResult<PracticeRecordModel>>() {
        });
    if (practiceRecordModel.isCode()) {
      practiceRecordList.clear();
      practiceRecordList.addAll(practiceRecordModel.getResult().getList());
      trainingRecordAdapter.updateListView(practiceRecordList);
    }
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }

  @Override protected void onError() {
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }
}
